<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/14 014
 * Time: 15:22
 */

namespace App\Command;

class RemoteControlWithUndo
{
    public $onCommands;
    public $offCommands;
    public $undoCommand;

    public function __construct()
    {
        $this->onCommands[] = new HasCommand();
        $this->offCommands[] = new HasCommand();

        $noCommand = new NoCommand();
        for($i=0;$i<7;$i++)
        {
            $this->onCommands[$i] = $noCommand;
            $this->offCommands[$i] = $noCommand;
        }

        $this->undoCommand = $noCommand;
    }

    public function setCommand($slot,$onCommand,$offCommand)
    {
        $this->onCommands[$slot] = $onCommand;
        $this->offCommands[$slot] = $offCommand;
    }

    public function onButtonWasPushed($slot)
    {
        $this->onCommands[$slot]->execute();
        $this->undoCommand = $this->onCommands[$slot];
    }

    public function offButtonWasPushed($slot)
    {
        $this->offCommands[$slot]->execute();
        $this->undoCommand = $this->offCommands[$slot];
    }

    public function undoButtonWasPushed()
    {
        $this->undoCommand->undo();
    }

    public function toString()
    {
        $stringBuff = "<br />------ Undo Remote Control ------<br />";
        for ($i = 0; $i< count($this->onCommands); $i++) {
            $stringBuff .= "[slot " . $i . "] " . get_class($this->onCommands[$i]) ." ". get_class($this->offCommands[$i]) . "<br />";
        }
        $stringBuff .= "[undo] " . get_class($this->undoCommand) . "<br />";
        return $stringBuff;
    }
}